import { Injectable } from '@angular/core';
import { Actor, Clue, Story, StoryEntity, Event, Group, Location, Stuff, Task, Panorama, Express, Episode, Scene, Audience, Note, Destiny } from 'src/classes/entities';
import { CommonService } from './common.service';
import { StoreService } from './store.service';

@Injectable({
  providedIn: 'root'
})
export class DataService {

  constructor(private store: StoreService) { }

  public newstory() {
    this.store.currentStory = new Story()
  }

  public addorupdate(array: StoryEntity[], item: StoryEntity): boolean {
    let index = array.findIndex(v => v.Id === item.Id)
    if (index > -1) {
      array.splice(index, 1)
    }
    array.push(item)
    return true
  }
  public removeorupdate(array: StoryEntity[], item: StoryEntity): boolean {
    let index = array.findIndex(v => v.Id === item.Id)
    if (index > -1) {
      array.splice(index, 1)
    }
    return true
  }
  public addEntity<T extends StoryEntity>(entity: T): boolean {
    if (entity === undefined || entity === null || entity.Id === '' || entity.Id === undefined || entity.Id === null)
      return false;
    if (entity instanceof Actor) {
      return this.addorupdate(this.store.currentStory.Actors, entity)

    }
    if (entity instanceof Clue) {
      return this.addorupdate(this.store.currentStory.Clues, entity)

    }
    if (entity instanceof Event) {
      return this.addorupdate(this.store.currentStory.Events, entity)
    }
    if (entity instanceof Group) {
      return this.addorupdate(this.store.currentStory.Groups, entity)
    }
    if (entity instanceof Location) {
      return this.addorupdate(this.store.currentStory.Locations, entity)
    }
    if (entity instanceof Stuff) {
      return this.addorupdate(this.store.currentStory.Stuffs, entity)
    }
    if (entity instanceof Task) {
      return this.addorupdate(this.store.currentStory.Tasks, entity)
    }
    return false
  }

  public saveEntity(entity) {

  }
  public removeEntity<T extends StoryEntity>(entity: T): boolean {
    if (entity === undefined || entity === null || entity.Id === '' || entity.Id === undefined || entity.Id === null)
      return false;
    if (entity instanceof Actor) {
      return this.removeorupdate(this.store.currentStory.Actors, entity)

    }
    if (entity instanceof Clue) {
      return this.removeorupdate(this.store.currentStory.Clues, entity)

    }
    if (entity instanceof Event) {
      return this.removeorupdate(this.store.currentStory.Events, entity)
    }
    if (entity instanceof Group) {
      return this.removeorupdate(this.store.currentStory.Groups, entity)
    }
    if (entity instanceof Location) {
      return this.removeorupdate(this.store.currentStory.Locations, entity)
    }
    if (entity instanceof Stuff) {
      return this.removeorupdate(this.store.currentStory.Stuffs, entity)
    }
    if (entity instanceof Task) {
      return this.removeorupdate(this.store.currentStory.Tasks, entity)
    }
    return false
  }

  public addNote(note: Note) {
    let index = this.store.currentStory.Notes.findIndex(v => v.NoteId === note.NoteId)
    if (index > -1) {
      //express.Name = CommonService.getFirstValidName(this.store.currentStory.Expresses.map(e => e.Name), express.Name)
      this.store.currentStory.Notes.splice(index, 1)
    }
    this.store.currentStory.Notes.push(note)
  }
  public addPanorama(panorama: Panorama): boolean {
    if (panorama === undefined || panorama.Name === '' || panorama.Name === undefined)
      return false
    let index = this.store.currentStory.Panoramas.findIndex(v => v.Name === panorama.Name)
    if (index > -1) {
      panorama.Name = CommonService.getFirstValidName(this.store.currentStory.Panoramas.map(e => e.Name), panorama.Name)
      //this.store.currentStory.Panoramas.splice(index, 1)
    }
    this.store.currentStory.Panoramas.push(panorama)
    console.log(this.store.currentStory.Panoramas);
  }

  public addExpress(express: Express): boolean {
    if (express === undefined || express.Name === '' || express.Name === undefined) {
      console.log('no valid express');
      return false
    }
    let index = this.store.currentStory.Expresses.findIndex(v => v.Name === express.Name)
    if (index > -1) {
      console.log('duplicate name:' + express.Name);
      express.Name = CommonService.getFirstValidName(this.store.currentStory.Expresses.map(e => e.Name), express.Name)
      //this.store.currentStory.Expresses.splice(index, 1)
    }
    this.store.currentStory.Expresses.push(express)
  }

  public getScenes(episode: Episode, express: Express): Scene[] {
    let ss: Scene[] = []

    episode.SceneNos.forEach(no => {
      express.Scenes.forEach(s => {
        if (s.SceneNumber === no)
          ss.push(s)
      })
    })
    return ss
  }

  public createEpisode(express: Express) {

    let e = new Episode()
    express.Episodes.push(e)
    return e
  }

  public createScene(express: Express) {

    let e = new Scene()
    express.Scenes.push(e)
    return e
  }
  public createAudience(express: Express) {

    let e = new Audience()
    express.Audiences.push(e)
    return e
  }

  public getDestiny(target: StoryEntity): Destiny {
    return new Destiny(target, this.store.currentStory.begin, this.store.currentStory.end, this.store.currentStory)


  }
}
